from flask import Flask,jsonify,request,Blueprint,session
from common.res import (getRes,getErrorRes,getSuccessRes,getDataRes)
from common.sqlInjectionSecurity import sqlEscape,sqlEscapeField
from DAL.organizationDAL import deviceDAL

org_device = Blueprint('org_device',__name__)

@org_device.route("/org_device/getlist",methods=['POST'])
def getlist():
    param = {
        'name': request.form.get('name') and sqlEscape(request.form.get('name')) or "",
        'ip': request.form.get('ip') and sqlEscape(request.form.get('ip')) or "",
        'page': request.form.get('page') and sqlEscape(request.form.get('page')) or 1,
        'limit': request.form.get('limit') and sqlEscape(request.form.get('limit')) or 20,
        'filed': request.form.get('filed') and sqlEscape(request.form.get('filed')) or 'id',
        'sort': request.form.get('sort') and sqlEscape(request.form.get('sort')) or 'ascending'
          }
    dal = deviceDAL()
    data,count = dal.getList(**param)
    if data:
        res = getSuccessRes(data=data,count=count)
    else:
        res = getSuccessRes(data=[], count=count)
    return jsonify(res)


@org_device.route("/org_device/delete_by_id",methods=['POST'])
def delete_by_id():
    param = {
        'id': request.form.get('id') or None
    }
    if not param['id']:
        res = getErrorRes("你输入的参数有错！")
        return jsonify(res)
    dal = deviceDAL()
    data = dal.deleteById(**param)
    res = getSuccessRes(data=data)
    return jsonify(res)

@org_device.route("/org_device/update",methods=['POST'])
def update():
    param = {
        'id': request.form.get('id') or None,
        'name': request.form.get('name') and sqlEscape(request.form.get('name')) or "",
        'ip': request.form.get('ip') and sqlEscape(request.form.get('ip')) or "",
    }
    if not param['id']:
        res = getErrorRes("参数id必须填写！")
        return jsonify(res)
    if not param['name']:
        res = getErrorRes("设备号必须填写！")
        return jsonify(res)
    if not param['ip']:
        res = getErrorRes("ip地址名称必须填写！")
        return jsonify(res)

    dal = deviceDAL()
    data = dal.update(**param)
    res = getSuccessRes(data=data)
    return jsonify(res)

@org_device.route("/org_device/notExistAdd",methods=['POST'])
def add():
    param = {
        'name': request.form.get('name') and sqlEscape(request.form.get('name')) or "",
        'ip': request.form.get('ip') and sqlEscape(request.form.get('ip')) or "",
    }
    if not param['name']:
        res = getErrorRes("设备号必须填写！")
        return jsonify(res)
    if not param['ip']:
        res = getErrorRes("ip地址名称必须填写！")
        return jsonify(res)
    dal = deviceDAL()
    data = dal.notExistAdd(**param)
    res = getSuccessRes(data=data)
    return jsonify(res)

@org_device.route("/org_device/getListAll",methods=['POST'])
def getlist_all():
    dal = deviceDAL()
    data = dal.getListAll()
    if data:
        res = getSuccessRes(data=data)
    else:
        res = getSuccessRes(data=[])
    return jsonify(res)

